//index.js
//获取应用实例
var app = getApp();
var Utils = app.CommonUtils;
var Keys = require('../../assets/data/keys.js');

Page({
  data: {
    pId: 0,
    isCollect: 0, //0:未收藏  1：收藏
    autoplay: true,
    interval: 3000,
    duration: 1000,
    goodsDetail: {},
    swiperCurrent: 0,
    hasMoreSelect: false,
    selectSize: "选择：",
    selectSizePrice: 0,
    shopNum: 0,
    hideShopPopup: true,
    buyNumber: 0,
    buyNumMin: 1,
    buyNumMax: 0,

    propertyChildIds: "",
    propertyChildNames: "",
    canSubmit: false, //  选中规格尺寸时候是否允许加入购物车
    shopCarInfo: {},
    shopType: "addShopCar",//购物类型，加入购物车或立即购买，默认为加入购物车
    statusType: ["商品详情", "查看评价"],
    currentType: 0,
    datas: '',
    show1 :false,
    show2 :true
  },

  //事件处理函数
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    if (curType == 0) {
      this.setData({
        show1: false,
        show2:true
      });
    } else if (curType == 1) {
      
      this.setData({
        show1: true,
        show2: false
      });
    }
  },

  onLoad: function (e) {
    var that = this;
    var _this = this;
    wx.showLoading({
      title: '数据加载中',
    })
    wx.request({
      url: Keys.data.url_TEST_ROOT +"ProductDetail.ashx",
      data: {
        DataType: "Product_Details",
        P_ID: e.id,
        U_ID: app.login_id
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading()
        //that.data.goodsDetail = res.data.data[0];
        that.setData({
          pId: e.id,
          goodsDetail: res.data.data[0],
          //selectSizePrice: res.data.data[0].HJP_LPrice.toString(),
          buyNumMax: 100000,
          buyNumber: 1,
          isCollect: res.data.data[0].IFCollection,
        });

      }
    })

    // wx.request({
    //   url: Keys.data.url_Product,
    //   data: {
    //     DataType: "Product_Comment",
    //     pid: e.id,
    //     page: "1"
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.isok == "false")
    //       _this.showZanTopTips(res.data.info);
    //     else {
    //       _this.setData({
    //         datas: res.data.datas
    //       })
    //       console.log(_this.data.datas)
    //     }
    //   }
    // })



  },

  /**
   * 收藏按钮
   */
  btnCollect: function (res) {
    var _this = this;
    var _str;
    console.log(res);
    if (_this.data.isCollect == 0){
      console.log("0");
      _str = "Collection_Add";
    } else if (_this.data.isCollect == 1){
      console.log("1");
      _str = "Collection_Cancel";
    }
    console.log(_str);
    wx.showLoading();
    wx.request({
      url: "https://www.chnode.xyz/API/Collection.ashx?",
      data: {
        DataType: _str,
        U_ID: app.login_id,
        P_ID: _this.data.pId,
        // "type": _this.data.isCollect,
      },
      success: function (res) {
        console.log(res);
        wx.hideLoading()
        console.log(res.data)
        if (res.data.isok == "true") {
          if (_this.data.isCollect == 0) {
            Utils.showToastWithImg("收藏成功", 1)
            _this.setData({
              isCollect: 1,
            })
          } else {
            Utils.showToastWithImg("已取消收藏", 1)
            _this.setData({
              isCollect: 0,
            })
          }
        } else {
          Utils.showToastWithImg(res.data.info, 2)
        }
      }
    })
  },

  goShopCar: function () {
    wx.reLaunch({
      url: "/pages/shop-cart/index"
    });
  },

  toAddShopCar: function () {
    this.setData({
      shopType: "addShopCar"
    })
    this.bindGuiGeTap();
  },

  tobuy: function () {
    this.setData({
      shopType: "tobuy"
    });
    this.bindGuiGeTap();
  },
  /**
   * 规格选择弹出框
   */
  bindGuiGeTap: function () {
    this.setData({
      hideShopPopup: false
    })
  },
  /**
   * 规格选择弹出框隐藏
   */
  closePopupTap: function () {
    this.setData({
      hideShopPopup: true
    })
  },
  numJianTap: function () {
    if (this.data.buyNumber > this.data.buyNumMin) {
      var currentNum = this.data.buyNumber;
      currentNum--;
      this.setData({
        buyNumber: currentNum
      })
    }
  },
  numJiaTap: function () {
    if (this.data.buyNumber < this.data.buyNumMax) {
      var currentNum = this.data.buyNumber;
      currentNum++;
      this.setData({
        buyNumber: currentNum
      })
    }
  },
  /**
   * 选择商品规格
   * @param {Object} e
   */
  labelItemTap: function (e) {
    var that = this;
    // 取消该分类下的子栏目所有的选中状态
    var childs = that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods;
    for (var i = 0; i < childs.length; i++) {
      that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[i].active = false;
    }
    // 设置当前选中状态
    that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[e.currentTarget.dataset.propertychildindex].active = true;
    // 获取所有的选中规格尺寸数据
    var needSelectNum = that.data.goodsDetail.properties.length;
    var curSelectNum = 0;
    var propertyChildIds = "";
    var propertyChildNames = "";
    for (var i = 0; i < that.data.goodsDetail.properties.length; i++) {
      childs = that.data.goodsDetail.properties[i].childsCurGoods;
      for (var j = 0; j < childs.length; j++) {
        if (childs[j].active) {
          curSelectNum++;
          propertyChildIds = propertyChildIds + that.data.goodsDetail.properties[i].id + ":" + childs[j].id + ",";
          propertyChildNames = propertyChildNames + that.data.goodsDetail.properties[i].name + ":" + childs[j].name + "  ";
        }
      }
    }
    var canSubmit = false;
    if (needSelectNum == curSelectNum) {
      canSubmit = true;
    }

    this.setData({
      goodsDetail: that.data.goodsDetail,
      canSubmit: canSubmit
    })
  },
  /**
  * 加入购物车
  */
  addShopCar: function () {
    var _this = this;

    if (this.data.buyNumber < _this.data.goodsDetail.P_MinQuantity) {
    
      wx.showModal({
        title: '提示',
        content: '购买数量不能少于' + _this.data.goodsDetail.P_MinQuantity,
        showCancel: false
      })
      return;
    }
    wx.request({
      url: Keys.data.url_TEST_ROOT + "Car.ashx",
      data: {
        DataType: 'Car_Add',
        U_ID: app.login_id,
        P_ID: _this.data.goodsDetail.P_ID,
        //price: _this.data.goodsDetail.HJP_LPrice,
        FirstCount: _this.data.buyNumber,
      },

      success: function (res) {
        console.log(res)
        if (res.data.isok == "true") {
          wx.showToast({
            title: '加入购物车成功',
            icon: 'success',
            duration: 2000
          })
        }
      },

    });
    this.closePopupTap();
  },
	/**
	  * 立即购买
	  */
  buyNow: function () {
    if (this.data.buyNumber < 1) {
      wx.showModal({
        title: '提示',
        content: '购买数量不能为0！',
        showCancel: false
      })
      return;
    }
    //组建立即购买信息
    var buyNowInfo = this.buliduBuyNowInfo();
    if (this.data.buyNumber < this.data.goodsDetail.P_MinQuantity) {
      wx.showModal({
        title: '提示',
        content: '购买数量不能少于' + this.data.goodsDetail.P_MinQuantity,
        showCancel: false
      })
      return;
    }
    // 写入本地存储
    wx.setStorage({
      key: "buyNowInfo",
      data: buyNowInfo
    })
    this.closePopupTap();

    wx.navigateTo({
      url: "/pages/to-pay-order/index?orderType=buyNow"
    })
  },
	/**
	 * 组建立即购买信息
	 */
  buliduBuyNowInfo: function () {
    console.log(this.data.goodsDetail.HJP_Name);
    var shopCarMap = {};
    shopCarMap.pic = this.data.goodsDetail.Image[0].WI_FileName;
    shopCarMap.name = this.data.goodsDetail.P_Name;
    shopCarMap.num = this.data.buyNumber;
    shopCarMap.price = this.data.goodsDetail.MoneyNum;
    shopCarMap.id = this.data.goodsDetail.P_ID;
    var buyNowInfo = {};
    if (!buyNowInfo.shopNum) {
      buyNowInfo.shopNum = 0;
    }
    if (!buyNowInfo.shopList) {
      buyNowInfo.shopList = [];
    }
    buyNowInfo.shopList.push(shopCarMap);
    return buyNowInfo;
  }
})
